package ro.sergiu.finder.models.grid;

import java.awt.Component;
import javax.swing.JColorChooser;
import javax.swing.JTabbedPane;
import javax.swing.table.TableCellRenderer;
import ro.sergiu.finder.models.property.AutoresizeMode;
import ro.sergiu.finder.models.property.SelectionMode;
import ro.sergiu.finder.utils.ComboEnumModel;

public class GridModelPanel extends JTabbedPane {

    private GridModel tableSettings;

    public GridModelPanel() {
        initComponents();
    }

    public GridModelPanel(GridModel tableSettings) {
        this.tableSettings = tableSettings;
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        colorChooser = new javax.swing.JColorChooser();
        comboEnumRenderer = new ro.sergiu.finder.utils.ComboEnumCellRenderer();
        designBehaviour = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        previewTable = new javax.swing.JTable(){
            @Override
            public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
                Component c = super.prepareRenderer(renderer, row, column);
                if (!isRowSelected(row)) { //  Alternate row color
                    row++; //0 is even, because is in base 0 I have to increment row number
                    c.setBackground(row % 2 == 0 ? evenColor.getBackground() : oddColor.getBackground());
                    c.setForeground(row % 2 == 0 ? evenColor.getForeground() : oddColor.getForeground());
                }
                return c;
            }
        };
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        filler1 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0), new java.awt.Dimension(32767, 0));
        filler8 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 35), new java.awt.Dimension(0, 30), new java.awt.Dimension(0, 35));
        selectionMode = new javax.swing.JComboBox();
        jPanel3 = new javax.swing.JPanel();
        filler6 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 5), new java.awt.Dimension(0, 5), new java.awt.Dimension(32767, 5));
        jPanel7 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        filler2 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0), new java.awt.Dimension(32767, 0));
        resizeMode = new javax.swing.JComboBox();
        filler4 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 5), new java.awt.Dimension(0, 5), new java.awt.Dimension(32767, 5));
        jPanel8 = new javax.swing.JPanel();
        sortableRecords = new javax.swing.JCheckBox();
        filler3 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0), new java.awt.Dimension(32767, 0));
        movableColumns = new javax.swing.JCheckBox();
        filler5 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 5), new java.awt.Dimension(0, 5), new java.awt.Dimension(32767, 5));
        jPanel4 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        evenSelector = new javax.swing.JButton();
        oddSelector = new javax.swing.JButton();
        jPanel6 = new javax.swing.JPanel();
        evenSelector1 = new javax.swing.JButton();
        oddSelector1 = new javax.swing.JButton();
        jPanel9 = new javax.swing.JPanel();
        filler7 = new javax.swing.Box.Filler(new java.awt.Dimension(180, 5), new java.awt.Dimension(180, 5), new java.awt.Dimension(180, 5));
        oddColor = new javax.swing.JLabel();
        evenColor = new javax.swing.JLabel();
        resetTableColors = new javax.swing.JButton();

        comboEnumRenderer.setText("comboEnumCellRenderer1");

        setToolTipText("");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(" Preview "));
        jPanel1.setLayout(new javax.swing.BoxLayout(jPanel1, javax.swing.BoxLayout.LINE_AXIS));

        previewTable.setAutoCreateRowSorter(true);
        previewTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {"Evangeline Carson",true,"(0181) 769 8049",101.797f,5181152218002829l},
                {"Patience Burks",true,"076 3813 8179",123.639f,4738434165343647l},
                {"Dorothy Guy",false,"0800 941558",165.334f,4353454824537037l},
                {"Ava Dyer",true,"(016977) 5569",129.494f,4310710091423245l},
                {"Callie Taylor",true,"0800 1111",100.401f,4212582218507308l},
                {"Lois Reese",false,"070 5905 1236",126.088f,4516532450774687l},
                {"Helen Moody",true,"0500 448171",136.653f,5196425591581509l},
                {"Aspen Mcfadden",true,"070 6042 6697",148.716f,4989968301076441l},
                {"Zelda Coleman",true,"0800 1111",185.605f,4340899044973770l},
                {"Charde Flowers",false,"0327 939 7506",123.886f,4506290541263293l},
                {"Sasha Hood",true,"(01727) 269745",195.457f,5124000672777188l},
                {"Lynn Christensen",true,"0999 370 5613",150.532f,5113737902049903l},
                {"Cora Wolfe",true,"(016977) 5714",105.741f,4454061687644570l},
                {"Kimberley Reid",false,"0845 46 40",180.242f,5110563251194543l},
                {"Sharon Nicholson",true,"0980 681 6813",146.794f,4662981855496762l}
            },
            new String [] {
                "Name", "Active", "Phone", "Revenue", "Credit card"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class

                , java.lang.Boolean.class

                , java.lang.Long.class

                , java.lang.Float.class

                , java.lang.Long.class
            };
            boolean[] canEdit = new boolean[]{
                false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types[columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit[columnIndex];
            }
        });
        jScrollPane1.setViewportView(previewTable);
        previewTable.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

        jPanel1.add(jScrollPane1);

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(" Selection "));
        jPanel2.setLayout(new javax.swing.BoxLayout(jPanel2, javax.swing.BoxLayout.LINE_AXIS));

        jLabel1.setText("Selection mode:");
        jLabel1.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 6, 0, 0));
        jPanel2.add(jLabel1);
        jPanel2.add(filler1);
        jPanel2.add(filler8);

        selectionMode.setModel(new ComboEnumModel(SelectionMode.class));
        selectionMode.setMaximumSize(new java.awt.Dimension(180, 20));
        selectionMode.setMinimumSize(new java.awt.Dimension(180, 20));
        selectionMode.setPreferredSize(new java.awt.Dimension(180, 20));
        selectionMode.setRenderer(comboEnumRenderer);
        selectionMode.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectionModeActionPerformed(evt);
            }
        });
        jPanel2.add(selectionMode);

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(" Columns "));
        jPanel3.setLayout(new javax.swing.BoxLayout(jPanel3, javax.swing.BoxLayout.Y_AXIS));
        jPanel3.add(filler6);

        jPanel7.setLayout(new javax.swing.BoxLayout(jPanel7, javax.swing.BoxLayout.LINE_AXIS));

        jLabel2.setText("Column resizable mode:");
        jLabel2.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 6, 0, 0));
        jPanel7.add(jLabel2);
        jPanel7.add(filler2);

        resizeMode.setModel(new ComboEnumModel(AutoresizeMode.class)
        );
        resizeMode.setMaximumSize(new java.awt.Dimension(180, 20));
        resizeMode.setMinimumSize(new java.awt.Dimension(180, 20));
        resizeMode.setPreferredSize(new java.awt.Dimension(180, 20));
        resizeMode.setRenderer(comboEnumRenderer);
        resizeMode.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                resizeModeActionPerformed(evt);
            }
        });
        jPanel7.add(resizeMode);

        jPanel3.add(jPanel7);
        jPanel3.add(filler4);

        jPanel8.setLayout(new javax.swing.BoxLayout(jPanel8, javax.swing.BoxLayout.LINE_AXIS));

        sortableRecords.setSelected(true);
        sortableRecords.setText("Sort records");
        sortableRecords.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sortableRecordsActionPerformed(evt);
            }
        });
        jPanel8.add(sortableRecords);
        jPanel8.add(filler3);

        movableColumns.setSelected(true);
        movableColumns.setText("Movable columns");
        movableColumns.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                movableColumnsActionPerformed(evt);
            }
        });
        jPanel8.add(movableColumns);

        jPanel3.add(jPanel8);
        jPanel3.add(filler5);

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(" Row colors "));

        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder(" Back "));
        jPanel5.setLayout(new java.awt.BorderLayout(5, 5));

        evenSelector.setText("Even");
        evenSelector.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                evenSelectorActionPerformed(evt);
            }
        });
        jPanel5.add(evenSelector, java.awt.BorderLayout.SOUTH);

        oddSelector.setText("Odd");
        oddSelector.setMaximumSize(new java.awt.Dimension(57, 23));
        oddSelector.setMinimumSize(new java.awt.Dimension(57, 23));
        oddSelector.setPreferredSize(new java.awt.Dimension(57, 23));
        oddSelector.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                oddSelectorActionPerformed(evt);
            }
        });
        jPanel5.add(oddSelector, java.awt.BorderLayout.NORTH);

        jPanel6.setBorder(javax.swing.BorderFactory.createTitledBorder(" Text "));
        jPanel6.setLayout(new java.awt.BorderLayout(5, 5));

        evenSelector1.setText("Even");
        evenSelector1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                evenSelector1ActionPerformed(evt);
            }
        });
        jPanel6.add(evenSelector1, java.awt.BorderLayout.SOUTH);

        oddSelector1.setText("Odd");
        oddSelector1.setMaximumSize(new java.awt.Dimension(57, 23));
        oddSelector1.setMinimumSize(new java.awt.Dimension(57, 23));
        oddSelector1.setPreferredSize(new java.awt.Dimension(57, 23));
        oddSelector1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                oddSelector1ActionPerformed(evt);
            }
        });
        jPanel6.add(oddSelector1, java.awt.BorderLayout.NORTH);

        jPanel9.add(filler7);

        oddColor.setBackground(new java.awt.Color(255, 255, 255));
        oddColor.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        oddColor.setText("Selected color for odd rows");
        oddColor.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        oddColor.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        oddColor.setMaximumSize(new java.awt.Dimension(180, 16));
        oddColor.setMinimumSize(new java.awt.Dimension(180, 16));
        oddColor.setOpaque(true);
        oddColor.setPreferredSize(new java.awt.Dimension(180, 16));
        jPanel9.add(oddColor);

        evenColor.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        evenColor.setText("Selected color for even rows");
        evenColor.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        evenColor.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        evenColor.setMaximumSize(new java.awt.Dimension(180, 16));
        evenColor.setMinimumSize(new java.awt.Dimension(180, 16));
        evenColor.setOpaque(true);
        evenColor.setPreferredSize(new java.awt.Dimension(180, 16));
        jPanel9.add(evenColor);

        resetTableColors.setText("Reset to default");
        resetTableColors.setMaximumSize(new java.awt.Dimension(180, 16));
        resetTableColors.setMinimumSize(new java.awt.Dimension(180, 16));
        resetTableColors.setPreferredSize(new java.awt.Dimension(180, 23));
        resetTableColors.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                resetTableColorsActionPerformed(evt);
            }
        });
        jPanel9.add(resetTableColors);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGap(5, 5, 5)
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel9, javax.swing.GroupLayout.PREFERRED_SIZE, 189, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(5, 5, 5))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGap(5, 5, 5)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel9, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(5, 5, 5))
        );

        javax.swing.GroupLayout designBehaviourLayout = new javax.swing.GroupLayout(designBehaviour);
        designBehaviour.setLayout(designBehaviourLayout);
        designBehaviourLayout.setHorizontalGroup(
            designBehaviourLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(designBehaviourLayout.createSequentialGroup()
                .addGap(0, 0, 0)
                .addGroup(designBehaviourLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 323, Short.MAX_VALUE)
                .addGap(0, 0, 0))
        );
        designBehaviourLayout.setVerticalGroup(
            designBehaviourLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(designBehaviourLayout.createSequentialGroup()
                .addGap(0, 0, 0)
                .addGroup(designBehaviourLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 391, Short.MAX_VALUE)
                    .addGroup(designBehaviourLayout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addGap(0, 0, 0))
        );

        addTab("Design & behaviour", null, designBehaviour, "");
    }// </editor-fold>//GEN-END:initComponents

    private void oddSelectorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_oddSelectorActionPerformed
        oddColor.setBackground(JColorChooser.showDialog(this, "Select color for odd field", oddColor.getBackground()));
        previewTable.repaint();
    }//GEN-LAST:event_oddSelectorActionPerformed

    private void evenSelectorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_evenSelectorActionPerformed
        evenColor.setBackground(JColorChooser.showDialog(this, "Select color for even field", evenColor.getBackground()));
        previewTable.repaint();
    }//GEN-LAST:event_evenSelectorActionPerformed

    private void selectionModeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectionModeActionPerformed
        previewTable.setSelectionMode(((SelectionMode) selectionMode.getSelectedItem()).getId());
    }//GEN-LAST:event_selectionModeActionPerformed

    private void resizeModeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_resizeModeActionPerformed
        previewTable.setAutoResizeMode(((AutoresizeMode) resizeMode.getSelectedItem()).getId());
    }//GEN-LAST:event_resizeModeActionPerformed

    private void sortableRecordsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sortableRecordsActionPerformed
        if (!sortableRecords.isSelected()) {
            previewTable.setRowSorter(null);
        }
        previewTable.setAutoCreateRowSorter(sortableRecords.isSelected());
    }//GEN-LAST:event_sortableRecordsActionPerformed

    private void movableColumnsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_movableColumnsActionPerformed
        previewTable.getTableHeader().setReorderingAllowed(movableColumns.isSelected());
    }//GEN-LAST:event_movableColumnsActionPerformed

    private void evenSelector1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_evenSelector1ActionPerformed
        evenColor.setForeground(JColorChooser.showDialog(this, "Select text color for even field", evenColor.getForeground()));
        previewTable.repaint();
    }//GEN-LAST:event_evenSelector1ActionPerformed

    private void oddSelector1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_oddSelector1ActionPerformed
        oddColor.setForeground(JColorChooser.showDialog(this, "Select text color for odd field", oddColor.getForeground()));
        previewTable.repaint();
    }//GEN-LAST:event_oddSelector1ActionPerformed

    private void resetTableColorsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_resetTableColorsActionPerformed
        oddColor.setForeground(tableSettings.getOddForeground());
        oddColor.setBackground(tableSettings.getOddBackground());
        evenColor.setForeground(tableSettings.getEvenForeground());
        evenColor.setBackground(tableSettings.getEvenBackground());
        previewTable.repaint();
    }//GEN-LAST:event_resetTableColorsActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JColorChooser colorChooser;
    private ro.sergiu.finder.utils.ComboEnumCellRenderer comboEnumRenderer;
    private javax.swing.JPanel designBehaviour;
    protected javax.swing.JLabel evenColor;
    private javax.swing.JButton evenSelector;
    private javax.swing.JButton evenSelector1;
    private javax.swing.Box.Filler filler1;
    private javax.swing.Box.Filler filler2;
    private javax.swing.Box.Filler filler3;
    private javax.swing.Box.Filler filler4;
    private javax.swing.Box.Filler filler5;
    private javax.swing.Box.Filler filler6;
    private javax.swing.Box.Filler filler7;
    private javax.swing.Box.Filler filler8;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JPanel jPanel9;
    private javax.swing.JScrollPane jScrollPane1;
    protected javax.swing.JCheckBox movableColumns;
    protected javax.swing.JLabel oddColor;
    private javax.swing.JButton oddSelector;
    private javax.swing.JButton oddSelector1;
    private javax.swing.JTable previewTable;
    private javax.swing.JButton resetTableColors;
    protected javax.swing.JComboBox resizeMode;
    protected javax.swing.JComboBox selectionMode;
    protected javax.swing.JCheckBox sortableRecords;
    // End of variables declaration//GEN-END:variables

}
